package com.itheima.demo;

import com.itheima.domain.Person;
import com.sun.xml.internal.ws.addressing.WsaActionUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UpdateDemo04 {
    public static void main(String[] args) throws Exception {

        //1.注册驱动(默认注册)
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        //协议：// ip地址：端口号/请求资源
        Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.31.129:3306/demo3", "root", "root");
        //3.编写SQL语句
        String sql = "select * from person";
        //4.创建编译对象
        Statement st = conn.createStatement();
        //5.执行SQL语句
        ResultSet res = st.executeQuery(sql);
        //6.遍历结果
        List<Person> list = new ArrayList<Person>(0);
        while (res.next()){
            Person p = new Person();
            p.setId(res.getInt("id"));
            p.setName(res.getString("name"));
            p.setAge(res.getString("age"));
            list.add(p);
        }

        //释放资源
        res.close();
        st.close();
        conn.close();

        System.out.println(list);
    }
}
